<?php
class ModelCatalogReview extends Model {		
	public function addReview($product_id, $data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "review SET author = '" . $this->db->escape($data['name']) . "', customer_id = '" . (int)$this->customer->getId() . "', product_id = '" . (int)$product_id . "', text = '" . $this->db->escape(strip_tags($data['text'])) . "', rating = '" . (int)$data['rating'] . "', date_added = NOW()");
	}
		
	public function getReviewsByProductId($product_id, $start = 0, $limit = 20) {
		$query = $this->db->query("SELECT r.review_id, r.author, r.rating, r.text, p.product_id, p.name, p.price, p.image, r.date_added FROM " . DB_PREFIX . "review r LEFT JOIN " . DB_PREFIX . "product p ON (r.product_id = p.product_id) " . " WHERE p.product_id = '" . (int)$product_id . "' AND p.date_available <= NOW() AND p.status = '1' AND r.status = '1' AND LENGTH(r.text)>0 AND p.language_id = '" . (int)$this->language->getId() . "' ORDER BY r.date_added DESC LIMIT " . (int)$start . "," . (int)$limit);
		
		return $query->rows;
	}
	
	public function getAverageRating($product_id) {
		$query = $this->db->query("SELECT AVG(rating) AS total FROM " . DB_PREFIX . "review WHERE status = '1' AND product_id = '" . (int)$product_id . "' GROUP BY product_id");
		
		if (isset($query->row['total'])) {
			return (float)$query->row['total'];
		} else {
			return 0;
		}
	}
	//Add code by Q-Hieu Oct 17 2012
	public function getTotalVote ($product_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "review WHERE status = '1' AND product_id = '" . (int)$product_id . "' GROUP BY product_id");
		if (isset($query->row['total'])) {
			return $query->row['total'];
		} else {
			return 0;
		}
	}	
	public function getCheckVote ($product_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "review WHERE customer_id= '" . (int)$this->customer->getID() . "' AND product_id = '" . (int)$product_id . "' GROUP BY product_id");
		if (isset($query->row['total'])) {
			return 1;
		} else {
			return 0;
		}
	}
	//End code by Q-Hieu Oct 17 2012
	
	public function getTotalReviews() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "review r LEFT JOIN " . DB_PREFIX . "product p ON (r.product_id = p.product_id) WHERE p.date_available <= NOW() AND p.status = '1' AND r.status = '1'");
		
		return $query->row['total'];
	}

	public function getTotalReviewsByProductId($product_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "review r LEFT JOIN " . DB_PREFIX . "product p ON (r.product_id = p.product_id) " . "WHERE p.product_id = '" . (int)$product_id . "' AND p.date_available <= NOW() AND p.status = '1' AND r.status = '1' AND p.language_id = '" . (int)$this->language->getId() . "'");
		
		return $query->row['total'];
	}
	//Add code by D-Ngan Aug 26 2012
	public function addRate($product_id,$rate){
		$query = $this->db->query('SELECT * from review where product_id='.$product_id.' AND customer_id='.$this->customer->getId().';');
		if(sizeof($query->rows)==0)
			$this->db->query("INSERT INTO " . DB_PREFIX . "review SET author = '" .$this->customer->getUsername(). "', customer_id = '" . (int)$this->customer->getId() . "', product_id = '" .$product_id. "', text = '', rating = '" . $rate. "', date_added = NOW()");
		else{
			$this->db->query('UPDATE review SET rating='.$rate.',date_added=NOW() WHERE product_id='.$product_id.' AND customer_id='.$this->customer->getId().';');
		}
	}
	//End code by D-Ngan Aug 26 2012
}
?>